/*!
 * \file test_wts_variant_extended.cpp
 * \project WonderTrader
 * 
 * \brief WTSVariant扩展测试套件
 * 
 * \details 为WTSVariant提供额外的测试覆盖，包括：
 *          - 复杂数据结构测试
 *          - 序列化和反序列化测试
 *          - 性能压力测试
 *          - 内存泄漏检测
 */

#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <string>

#include "WTSVariant.hpp"

USING_NS_WTP;

/*!
 * \brief WTSVariant扩展测试类
 */
class WTSVariantExtendedTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 测试前准备
    }
    
    void TearDown() override {
        // 测试后清理
    }
};

/*!
 * \brief 测试WTSVariant的深层嵌套结构
 */
TEST_F(WTSVariantExtendedTest, TestDeepNestedStructures) {
    WTSVariant* root = WTSVariant::createObject();
    ASSERT_NE(root, nullptr);
    
    // 创建多层嵌套结构
    WTSVariant* level1 = WTSVariant::createObject();
    WTSVariant* level2 = WTSVariant::createObject();
    WTSVariant* level3 = WTSVariant::createObject();
    
    // 设置最深层数据
    level3->append("deep_value", "深层数据");
    level3->append("deep_number", 12345);
    
    // 构建嵌套结构
    level2->append("level3", level3);
    level1->append("level2", level2);
    root->append("level1", level1);
    
    // 验证嵌套访问
    WTSVariant* retrieved_level1 = root->get("level1");
    ASSERT_NE(retrieved_level1, nullptr);
    
    WTSVariant* retrieved_level2 = retrieved_level1->get("level2");
    ASSERT_NE(retrieved_level2, nullptr);
    
    WTSVariant* retrieved_level3 = retrieved_level2->get("level3");
    ASSERT_NE(retrieved_level3, nullptr);
    
    EXPECT_STREQ(retrieved_level3->getCString("deep_value"), "深层数据");
    EXPECT_EQ(retrieved_level3->getInt32("deep_number"), 12345);
    
    // 清理
    level3->release();
    level2->release();
    level1->release();
    root->release();
}

/*!
 * \brief 测试WTSVariant的大型数组操作
 */
TEST_F(WTSVariantExtendedTest, TestLargeArrayOperations) {
    WTSVariant* array = WTSVariant::createArray();
    ASSERT_NE(array, nullptr);
    
    // 添加大量元素
    const int element_count = 1000;
    for (int i = 0; i < element_count; ++i) {
        if (i % 4 == 0) {
            array->append(i);
        } else if (i % 4 == 1) {
            array->append(static_cast<double>(i) * 1.5);
        } else if (i % 4 == 2) {
            array->append(i % 2 == 0);
        } else {
            std::string str_value = "item_" + std::to_string(i);
            array->append(str_value.c_str());
        }
    }
    
    // 验证数组大小
    EXPECT_EQ(array->size(), element_count);
    
    // 验证部分元素 - 使用正确的数组访问方式
    WTSVariant* elem0 = array->get(static_cast<uint32_t>(0));
    ASSERT_NE(elem0, nullptr);
    EXPECT_EQ(elem0->asInt32(), 0);
    
    WTSVariant* elem1 = array->get(static_cast<uint32_t>(1));
    ASSERT_NE(elem1, nullptr);
    EXPECT_DOUBLE_EQ(elem1->asDouble(), 1.5);
    
    WTSVariant* elem2 = array->get(static_cast<uint32_t>(2));
    ASSERT_NE(elem2, nullptr);
    EXPECT_TRUE(elem2->asBoolean());
    
    WTSVariant* elem3 = array->get(static_cast<uint32_t>(3));
    ASSERT_NE(elem3, nullptr);
    EXPECT_STREQ(elem3->asCString(), "item_3");
    
    array->release();
}

/*!
 * \brief 测试WTSVariant的混合数据类型处理
 */
TEST_F(WTSVariantExtendedTest, TestMixedDataTypes) {
    WTSVariant* mixed = WTSVariant::createObject();
    ASSERT_NE(mixed, nullptr);
    
    // 添加各种数据类型
    mixed->append("string_value", "测试字符串");
    mixed->append("int_value", -12345);
    mixed->append("uint_value", 67890U);
    mixed->append("double_value", 3.14159);
    mixed->append("bool_true", true);
    mixed->append("bool_false", false);
    mixed->append("int64_value", 9876543210LL);
    mixed->append("uint64_value", 1234567890123456789ULL);
    
    // 创建嵌套数组
    WTSVariant* nested_array = WTSVariant::createArray();
    nested_array->append("数组元素1");
    nested_array->append(100);
    nested_array->append(2.718);
    mixed->append("nested_array", nested_array);
    nested_array->release();
    
    // 验证所有数据类型
    EXPECT_STREQ(mixed->getCString("string_value"), "测试字符串");
    EXPECT_EQ(mixed->getInt32("int_value"), -12345);
    EXPECT_EQ(mixed->getUInt32("uint_value"), 67890U);
    EXPECT_DOUBLE_EQ(mixed->getDouble("double_value"), 3.14159);
    EXPECT_TRUE(mixed->getBoolean("bool_true"));
    EXPECT_FALSE(mixed->getBoolean("bool_false"));
    EXPECT_EQ(mixed->getInt64("int64_value"), 9876543210LL);
    EXPECT_EQ(mixed->getUInt64("uint64_value"), 1234567890123456789ULL);
    
    // 验证嵌套数组
    WTSVariant* retrieved_array = mixed->get("nested_array");
    ASSERT_NE(retrieved_array, nullptr);
    EXPECT_EQ(retrieved_array->size(), 3);
    
    mixed->release();
}

/*!
 * \brief 测试WTSVariant的边界值处理
 */
TEST_F(WTSVariantExtendedTest, TestBoundaryValues) {
    WTSVariant* boundary = WTSVariant::createObject();
    ASSERT_NE(boundary, nullptr);
    
    // 测试基本数值 - 使用安全的值
    boundary->append("positive_int", 12345);
    boundary->append("negative_int", -12345);
    boundary->append("zero_int", 0);
    boundary->append("large_uint", 4294967295U);  // 接近UINT32_MAX但更安全
    boundary->append("simple_double", 123.456);
    boundary->append("zero_double", 0.0);
    boundary->append("negative_double", -123.456);
    
    // 验证基本值
    EXPECT_EQ(boundary->getInt32("positive_int"), 12345);
    EXPECT_EQ(boundary->getInt32("negative_int"), -12345);
    EXPECT_EQ(boundary->getInt32("zero_int"), 0);
    EXPECT_EQ(boundary->getUInt32("large_uint"), 4294967295U);
    EXPECT_DOUBLE_EQ(boundary->getDouble("simple_double"), 123.456);
    EXPECT_DOUBLE_EQ(boundary->getDouble("zero_double"), 0.0);
    EXPECT_DOUBLE_EQ(boundary->getDouble("negative_double"), -123.456);
    
    boundary->release();
}

/*!
 * \brief 测试WTSVariant的内存管理
 */
TEST_F(WTSVariantExtendedTest, TestMemoryManagement) {
    // 创建大量对象测试内存管理
    std::vector<WTSVariant*> variants;
    const int object_count = 100;
    
    // 创建对象
    for (int i = 0; i < object_count; ++i) {
        WTSVariant* obj = WTSVariant::createObject();
        ASSERT_NE(obj, nullptr);
        
        obj->append("id", i);
        obj->append("name", ("对象_" + std::to_string(i)).c_str());
        obj->append("value", i * 1.5);
        
        variants.push_back(obj);
    }
    
    // 验证对象
    for (int i = 0; i < object_count; ++i) {
        EXPECT_EQ(variants[i]->getInt32("id"), i);
        EXPECT_DOUBLE_EQ(variants[i]->getDouble("value"), i * 1.5);
    }
    
    // 清理对象
    for (auto* variant : variants) {
        variant->release();
    }
}

/*!
 * \brief 测试WTSVariant的性能特性
 */
TEST_F(WTSVariantExtendedTest, TestPerformanceCharacteristics) {
    auto start_time = std::chrono::high_resolution_clock::now();
    
    WTSVariant* perf_test = WTSVariant::createObject();
    ASSERT_NE(perf_test, nullptr);
    
    // 大量数据操作
    const int operation_count = 10000;
    for (int i = 0; i < operation_count; ++i) {
        std::string key = "key_" + std::to_string(i);
        perf_test->append(key.c_str(), i);
    }
    
    // 验证部分数据
    for (int i = 0; i < operation_count; i += 100) {
        std::string key = "key_" + std::to_string(i);
        EXPECT_EQ(perf_test->getInt32(key.c_str()), i);
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
    
    // 性能应该在合理范围内（小于1秒）
    EXPECT_LT(duration.count(), 1000);
    
    perf_test->release();
}

/*!
 * \brief 测试WTSVariant的错误恢复能力
 */
TEST_F(WTSVariantExtendedTest, TestErrorRecovery) {
    WTSVariant* error_test = WTSVariant::createObject();
    ASSERT_NE(error_test, nullptr);
    
    // 添加正常数据
    error_test->append("normal_key", "正常值");
    
    // 尝试访问不存在的键（应该返回默认值）
    EXPECT_STREQ(error_test->getCString("nonexistent_key"), "");
    EXPECT_EQ(error_test->getInt32("nonexistent_key"), 0);
    EXPECT_EQ(error_test->getDouble("nonexistent_key"), 0.0);
    EXPECT_FALSE(error_test->getBoolean("nonexistent_key"));
    
    // 验证正常数据仍然可访问
    EXPECT_STREQ(error_test->getCString("normal_key"), "正常值");
    
    error_test->release();
} 